package com.template.controller;


import com.template.entity.User;
import com.template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yumuyang
 * @since 2021-11-01
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/deleteUser")
    public String deleteUser(@RequestParam("id") Integer id,
                             @RequestParam(value = "pn", defaultValue = "1") Integer pn,
                             RedirectAttributes redirectAttributes) {
        userService.deleteUserById(id);

        redirectAttributes.addAttribute("pn", pn);

        return "redirect:/userList";
    }

    @PostMapping("/addUser")
    public String addUser(User user) {
        userService.saveUser(user);

        return "redirect:/userList";
    }

    @PostMapping("/updateUser")
    public String updateUser(@RequestParam(value = "pn", defaultValue = "1") Integer pn,
                             User user,
                             RedirectAttributes redirectAttributes) {
        userService.updateUser(user);

        redirectAttributes.addAttribute("pn", pn);

        return "redirect:/userList";
    }

    @PostMapping("/queryUser")
    public String queryUser(@RequestParam(value = "departmentid", defaultValue = "1") Integer departmentid,
                            @RequestParam(value = "keyword") String keyword) {
        System.out.println(departmentid + keyword);

        return "redirect:/userList";
    }

}

